package com.hs.controller;

import com.hs.service.MenuService;
import com.hs.service.RoleService;
import com.hs.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/*
 * @Author:iWeeper
 * @Date:2022/6/21 22:58
 * @Version:1.0
 * */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("menuList")
    public String menuList(){
        return "manager/menu-list";
    }

    @RequestMapping("/menuList/data")
    protected void menuListJson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service方法查询菜单列表
        response.setCharacterEncoding("UTF-8");
        List<Map<String,Object>> list = menuService.queryAll();
        ResultUtils.success(response,list,"ok");
    }

    @RequestMapping("/setMenus")
    protected void setMenus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取menuIds
        response.setCharacterEncoding("UTF-8");
        String menuIds = request.getParameter("menuIds");
        String roleId = request.getParameter("roleId");
        //执行设置菜单权限的方法
        try{
            Boolean result = roleService.saveMenus(menuIds,Integer.parseInt(roleId));
            if(result){
                ResultUtils.success(response,"设置成功！");
            }else{
                ResultUtils.error(response,71,"参数丢失，设置失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            ResultUtils.error(response,70,"系统异常，请联系管理员");
        }
    }
}
